package com.dup.jxlh.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dup.jxlh.admin.entities.model.Admin;
import com.dup.jxlh.admin.entities.model.Contract;
import com.dup.jxlh.admin.entities.vo.AdminVO;
import com.dup.jxlh.admin.entities.auth.AuthUser;
import com.dup.jxlh.admin.entities.vo.RoleVO;
import com.dup.jxlh.admin.mapper.*;
import com.dup.jxlh.admin.service.AdminService;
import com.dup.jxlh.admin.service.auth.AuthService;
import com.dup.jxlh.admin.utils.RedisUtil;
import com.dup.jxlh.admin.validator.Phone;
import com.dup.jxlh.admin.validator.RoleId;
import com.dup.jxlh.api.entities.CommonResult;
import com.dup.jxlh.api.entities.DictionaryData;
import com.dup.jxlh.api.entities.PaginationData;
import com.dup.jxlh.api.enums.LevelEnum;
import com.dup.jxlh.api.enums.ResultEnum;
import com.dup.jxlh.api.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Map;

@RestController
@RequestMapping("/test")
@Validated
public class TestController {
    @Resource
    private AuthMapper authMapper;

    @Resource
    private AdminMapper adminMapper;

    @Resource
    private CompanyMapper companyMapper;

    @Resource
    private RedisUtil redisUtil;

    @Autowired
    private AuthService authService;

    @Autowired
    private AdminService adminService;

    @GetMapping("/test1")
    @Cacheable(value = {"item"})
    public Object Test1(String phone) {
        return authMapper.findByPhone(phone);
    }

    @GetMapping("/test2")
//    @PreAuthorize("hasAuthority('ROLE_Admin')")
    public CommonResult Test2() {
        authService.createCurrentAuthPermFlag("admin");
        return ResultUtil.result(ResultEnum.SUCCESS);
    }

    @GetMapping("/test3")
    public CommonResult Test3(String str1, String str2) {
        redisUtil.set("reids:test", "这是一个redis测试");
        return ResultUtil.result(ResultEnum.SUCCESS, redisUtil.get("reids:test"));
    }

    @GetMapping("/test4")
    public CommonResult Test4() {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        return ResultUtil.result(ResultEnum.SUCCESS, auth);
    }

    @GetMapping("/test5")
    @PreAuthorize("hasAuthority('ROLE_Admin')")
    public Object Test5(@AuthenticationPrincipal AuthUser authUser) {
        return ResultUtil.result(ResultEnum.SUCCESS, authUser);
    }

    @PostMapping("/test6")
    public Object Test6() {

        ArrayList<DictionaryData> levelEnums = new ArrayList<>();
        DictionaryData dictionaryData = new DictionaryData();
        for (LevelEnum stateEnum: LevelEnum.values()){
            dictionaryData.setId(stateEnum.getCode());
            dictionaryData.setName(stateEnum.getName());
            levelEnums.add(dictionaryData);
        }
        return levelEnums;
    }

    @PostMapping("/test7")
    public Object Test7(BigInteger id) {
        return adminService.getAdminInfo(id);
    }

    @PostMapping("/test8")
    public Object Test8() {
        return adminService.updateAdminPassword(BigInteger.valueOf(1),"123456");
    }

    @PostMapping("/test9")
    public Object Test9(PaginationData paginationData,Admin admin) {
        return paginationData;
    }

    @Resource
    private RoleMapper roleMapper;

    @PostMapping("/test10")
    public Object Test10(@Validated Admin admin) {
        return admin;
    }

    @PostMapping("/test11")
    public Object Test11(@RoleId BigInteger roleId) {
        return roleId;
    }

    @Resource
    private ContractMapper contractMapper;

    @PostMapping("/test12")
    public Object Test12(Contract contract) {
        contractMapper.insert(contract);
        return contractMapper.selectById(contract.getId());
    }
}
